Python 查看模块的帮助文档,方法和帮助信息 您所在的位置:网站首页 python 查看模块 Python 查看模块的帮助文档,方法和帮助信息

Python 查看模块的帮助文档,方法和帮助信息

2023-08-28 12:05| 来源: 网络整理| 查看: 265

参考链接:https://blog.csdn.net/u013810296/article/details/55509284

这里介绍下python自带的查看帮助功能,可以在编程时不中断地迅速找到所需模块和函数的使用方法

查看方法 通用帮助函数help()

python中的help()类似unix中的man指令,熟悉后会对我们的编程带来很大帮助

进入help帮助文档界面,根据屏幕提示可以继续键入相应关键词进行查询,继续键入modules可以列出当前所有安装的模块:

help> modules Please wait a moment while I gather a list of all available modules... AutoComplete _pyio filecmp pyscreeze AutoCompleteWindow _random fileinput pytweening ...... Enter any module name to get more help. Or, type "modules spam" to search for modules whose name or summary contain the string "spam".

可以继续键入相应的模块名称得到该模块的帮助信息。 这是python的通用的查询帮助,可以查到几乎所有的帮助文档,但我们很多时候不需要这样层级式地向下查询,接下来会介绍如何直接查询特定的模块和函数帮助信息。

例如要查询math模块的使用方法,可以如下操作:(输出的多行信息可通过q键退出)

>>> help(math)

  使用help(module_name)时首先需要import该模块,有些教程中不进行导入而在模块名中加入引号help('module_name'),这种方法可能会带来问题,大家可以用math模块测试,建议使用先导入再使用help()函数查询

查看内建模块sys.bultin_modulenames >>> import sys >>> sys.builtin_module_names ('_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', ... 'zlib') >>>

  

查询函数信息 查看模块下所有函数dir(module_name)

如我们需要列举出math模块下所有的函数名称,同样需要首先导入该模块

>>> dir(math) ['__doc__', '__loader__', '__name__',...] >>>

  

查看模块下特定函数信息help(module_name.func_name)

注意

  func_name后面不要加(),因为python的语法默认加了括号后就运行函数

  相应的模块要导入

>>> help(math.sin) Help on built-in function sin in module math: sin(...) sin(x) Return the sine of x (measured in radians). >>> #例2 >>> help(random.randint()) Traceback (most recent call last): File "", line 1, in TypeError: randint() missing 2 required positional arguments: 'a' and 'b' >>> help(random.randint) Help on method randint in module random: randint(a, b) method of random.Random instance Return random integer in range [a, b], including both end points.

  

  

Python导入的包可以通过bagname.__all__查看所有方法但是这个有时不太好用,通过help(bagname.funcname)查看方法介绍

>>> help(random.seed) Help on method seed in module random: seed(a=None, version=2) method of random.Random instance Initialize internal state from hashable object. None or no argument seeds from current time or from an operating system specific randomness source if available. If *a* is an int, all bits are used. For version 2 (the default), all of the bits are used if *a* is a str, bytes, or bytearray. For version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

  



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有